package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

//IDEA中自动导包真好
public class AddOperation implements IOperation {

    @Override
    public void work(BookList bookList) {
        Scanner scanner=new Scanner(System.in);

        System.out.print("请输入书名：");
        String name=scanner.nextLine();
        System.out.print("请输入作者：");
        String author=scanner.nextLine();
        System.out.print("请输入类型：");
        String type=scanner.nextLine();
        System.out.print("请输入价格：");
        double price=scanner.nextDouble();

        //检查是否已经存在此书
        int currentSize=bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            if(name.equals(bookList.getBooks(i).getName())) {
                System.out.println("添加书籍已存在！");
                System.out.println(bookList.getBooks(i));
                return;
            }
        }
        //简单处理：每次添加到书架的后一个位置上
        Book book=new Book(name,author,price,type);
        bookList.setBook(book);
        bookList.setUsedSize(currentSize+1);
        System.out.println("添加图书成功！");
    }
}
